package com.project.slsx.activity;


import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.adapter.GoodsLossAdapter;
import com.project.slsx.bean.GoodsLossBean;
import com.project.slsx.bean.GoodsLossResultBean;
import com.project.slsx.utils.DialogUtils;
import com.project.slsx.utils.ParamsUtils;
import com.project.slsx.widget.dialog.BaseNiceDialog;
import com.project.slsx.widget.dialog.ViewConvertListener;
import com.project.slsx.widget.dialog.ViewHolder;

import org.angmarch.views.NiceSpinner;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品损耗
 */
public class GoodsLossActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = GoodsLossActivity.class.getSimpleName();
    private String keyword = "";
    private String goods_id = "";
    private String option_id = "";
    private String loss_type = "";
    private String number = "";
    private String amount = "";

    private GoodsLossAdapter adapter;
    private ArrayList<String> strings;
    private int selectedIndex = 0;
    private BaseNiceDialog loadingView;
    private BaseNiceDialog sunhaoDialog;
    private NiceSpinner niceSpinner;

    @Override
    public int getLayoutId() {
        return R.layout.activity_goods_loss;
    }

    @Override
    public void initView() {
        findViewById(R.id.rl_back).setOnClickListener(v -> finish());
        TextView title = findViewById(R.id.tv_title);
        title.setText("损耗");
        TextView tvRight = findViewById(R.id.tv_right);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText("收银");

        EditText edLoss = findViewById(R.id.ed_loss_search);
        edLoss.addTextChangedListener(new MyLossWatcher());
        findViewById(R.id.btn_loss_search).setOnClickListener(this);
        findViewById(R.id.btn_loss_list).setOnClickListener(this);

        RecyclerView rv = findViewById(R.id.recyclerview_goods_loss);
        rv.setLayoutManager(new LinearLayoutManager(mContext));
//        View header = View.inflate(mContext, R.layout.layout_item_goods_loss_header, null);
//        adapter.setHeaderView(header);
        adapter = new GoodsLossAdapter(R.layout.layout_item_goods_loss);
        rv.setAdapter(adapter);
    }

    @Override
    public void initData() {
        loadingView = showLoadingView();

        //损耗类型
        strings = new ArrayList<>();
        strings.add("退货");
        strings.add("过期");
        strings.add("鼠害");
        strings.add("漏气");
        strings.add("次品");
        strings.add("自用");
        adapter.setOnItemClickListener((adapter, view, position) -> {
            selectedIndex = position;
//            DialogUtils.showDialog(R.layout.dialog_shunhao, 320, 380, true,
//                    lossListener, getSupportFragmentManager());
            sunhaoDialog = DialogUtils.showDialog(R.layout.dialog_shunhao,
                    320, 320, true, lossListener);
            sunhaoDialog.show(getSupportFragmentManager());
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.btn_loss_search:
                if (TextUtils.isEmpty(keyword)) {
                    showToast(mContext, "请输入搜索关键字");
                    return;
                }
                doLossSearch();
                break;

            case R.id.btn_loss_list:

                doLossList();
                break;
        }
    }

    /**
     * 损耗列表
     */
    private void doLossList() {
        startActivity(new Intent(mContext, GoodsLossListActivity.class));
    }

    /**
     * 损耗搜索
     */
    private void doLossSearch() {
        loadingView.show(getSupportFragmentManager());
        HttpParams losssearchParams = ParamsUtils.getLosssearchParams();
        losssearchParams.put("keyword", keyword);
        OkGo.<String>post(Constans.BASEURL).tag("losssearch").params(losssearchParams).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                GoodsLossBean goodsLossBean = JSONObject.parseObject(response.body(), GoodsLossBean.class);
                if (goodsLossBean.getErrno() == Constans.SUCCESS) {
                    showToast(mContext, goodsLossBean.getErrmsg());
                    adapter.setNewData(goodsLossBean.getResult());
                } else {
                    showToast(mContext, goodsLossBean.getErrmsg());
                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);

            }

            @Override
            public void onFinish() {
                super.onFinish();
                loadingView.dismiss();
            }
        });
    }

    /**
     * 商品损耗提交
     */
    private void doGoodsLoss() {
        loadingView.show(getSupportFragmentManager());
        HttpParams goodslossParams = ParamsUtils.getGoodslossParams();
        List<GoodsLossBean.ResultBean> data = adapter.getData();
        goodslossParams.put("goods_id", data.get(selectedIndex).getGoods_id());
        goodslossParams.put("option_id", data.get(selectedIndex).getOption_id());
        goodslossParams.put("loss_type", loss_type);
        goodslossParams.put("number", number);
        goodslossParams.put("amount", amount);
        OkGo.<String>post(Constans.BASEURL).tag("goodsloss").params(goodslossParams).execute(new StringCallback() {

            @Override
            public void onSuccess(Response<String> response) {
                Log.i(TAG, "onSuccess: " + response.body());
                GoodsLossResultBean goodsLossResultBean = JSONObject.parseObject(response.body(), GoodsLossResultBean.class);
                //预留
//                if (goodsLossResultBean.getErrno() == Constans.SUCCESS) {
////                    sunhaoDialog.dismiss();
////                }
                showToast(mContext, goodsLossResultBean.getErrmsg());
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                showToast(mContext, "网络错误");
                Log.i(TAG, "onError: ");
            }

            @Override
            public void onFinish() {
                super.onFinish();
                loadingView.dismiss();
            }
        });
    }

    private class MyLossWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            keyword = s.toString().trim();
        }
    }

    private ViewConvertListener lossListener = new ViewConvertListener() {
        @Override
        protected void convertView(ViewHolder holder, BaseNiceDialog dialog) {
            LinearLayout convertView = (LinearLayout) holder.getConvertView();
            niceSpinner = convertView.findViewById(R.id.nice_spinner);
            EditText number = convertView.findViewById(R.id.ed_goods_loss_number);
            EditText price = convertView.findViewById(R.id.ed_goods_loss_price);
            niceSpinner.attachDataSource(strings);
            niceSpinner.setTextColor(Color.BLACK);
            niceSpinner.setBackground(getResources().getDrawable(R.drawable.bg_ed_login_corners_10));
            niceSpinner.setTextSize(10);
            niceSpinner.addOnItemClickListener((parent, view, position, id) -> {
                loss_type = strings.get(position);
            });

            number.addTextChangedListener(new NumberTextWathcr());
            price.addTextChangedListener(new PriceTextWatcher());
            convertView.findViewById(R.id.btn_goods_loss_ok).setOnClickListener(v -> {
                doGoodsLoss();
            });
            convertView.findViewById(R.id.btn_goods_loss_cancel).setOnClickListener(v -> {
                sunhaoDialog.dismiss();
            });
        }
    };


    private class NumberTextWathcr implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {


        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            number = s.toString().trim();
        }
    }

    private class PriceTextWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            amount = s.toString().trim();
        }
    }
}
